package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.UserLocation;
import com.tanhua.domain.vo.UserLocationVo;
import com.tanhua.dubbo.api.mongo.UserLocationApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;


@Service
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveLocation(Double latitude, Double longitude, String addrStr, Long userId) {
        //1. 查询当前用户是否有地理位置记录
        Query query = new Query(Criteria.where("userId").is(userId));
        if(!mongoTemplate.exists(query, UserLocation.class)){
            //2. 如果没有该用户的记录，直接保存
            UserLocation userLocation = new UserLocation();
            userLocation.setUserId(userId);
            userLocation.setCreated(System.currentTimeMillis());
            GeoJsonPoint location = new GeoJsonPoint(longitude,latitude);
            userLocation.setLocation(location);
            userLocation.setAddress(addrStr);
            userLocation.setUpdated(System.currentTimeMillis());
            userLocation.setLastUpdated(System.currentTimeMillis());
            mongoTemplate.save(userLocation);
        }else{
            // 3. 如果有该用户的记录，更新
            Update update = new Update();
            GeoJsonPoint location = new GeoJsonPoint(longitude,latitude);
            update.set("location",location);
            update.set("address",addrStr);
            update.set("updated",System.currentTimeMillis());
            update.set("lastUpdated",System.currentTimeMillis());
            mongoTemplate.updateFirst(query,update,UserLocation.class);
        }



    }

    /**
     * 搜索附件的人
     * @param userId
     * @param distance
     * @return
     */
    @Override
    public List<UserLocationVo> searchNear(Long userId, Long distance) { //distance 单位是米
        //1.查询当前我所在的位置信息
        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        GeoJsonPoint location = userLocation.getLocation();

        //2. 定义好圆半径
        Distance distanceObj = new Distance(distance/1000, Metrics.KILOMETERS);

        //3. 构造圆形范范围
        Circle circle = new Circle(location,distanceObj);

        //4、 使用mongodb搜索范围
        Query nearQuery = new Query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocationList = mongoTemplate.find(nearQuery, UserLocation.class);

        //4。把UserLocation转换为UserLocationVo对象返回
        return UserLocationVo.formatToList(userLocationList);
    }
}
